$(document).ready(function() {
  $(".navegacion").show();
  TABLE.paginate('.tablaPaginacion', 10);
});

var TABLE = {};

TABLE.paginate = function(table, pageLength) {
  // 1. Set up paging information
  var $table = $(table);
  var $rows = $table.find('tbody > tr');
  var numPages = Math.ceil($rows.length / pageLength) - 1;
  var actual = 0;
  
  // 2. Set up the navigation controls
  var $nav = $table.parents('.paginacion').find('.navegacion ul');
  var $back = $nav.find('li:first-child a');
  var $next = $nav.find('li:last-child a');
  
  
  
  $nav.find('a.pagina-actual strong').text(actual + 1);
  $nav.find('a.pagina-actual span').text(numPages + 1);
  $back.addClass('paging-disabled').click(function() {
      pagination('<');
    });
  $next.click(function() {
    pagination('>');
  });
  
  // 3. Show initial rows
  $rows.hide().slice(0,pageLength).show();
   
  pagination = function (direction){
    reveal = function (actual){
      $back.removeClass('paging-disabled');
      $next.removeClass('paging-disabled');
      
      $rows.hide().slice(actual * pageLength, actual * pageLength + pageLength).show();
        
      $nav.find('a.pagina-actual strong').text(actual + 1);
    };
    
    // 4. Move previous and next  
  	if (direction == '<') { // previous  	
      if (actual > 1) {
        reveal(actual -= 1);        
      }
      else if (actual == 1) {
        reveal (actual -= 1);
        $back.addClass('paging-disabled');
      }
    } else {
      if (actual < numPages - 1) {
        reveal(actual += 1);
      }
      else if (actual == numPages - 1) {
        reveal(actual += 1);
        $next.addClass('paging-disabled');
      }
    }
  }
  
}
